import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../models/product_model.dart';
import '../../../providers/product_provider.dart';

class ProductDetailController extends GetxController {
  final ProductProvider productProvider = ProductProvider();
  
  // 可观察变量
  final RxBool isLoading = true.obs;
  final RxInt productId = 0.obs;
  final Rx<Product?> product = Rx<Product?>(null);
  
  @override
  void onInit() {
    super.onInit();
    
    // 从路由参数中获取产品ID
    if (Get.arguments is Map && Get.arguments.containsKey('productId')) {
      final id = Get.arguments['productId'];
      if (id is int) {
        productId.value = id;
        loadProductDetail();
      }
    }
  }
  
  // 加载产品详情
  Future<void> loadProductDetail() async {
    try {
      isLoading.value = true;
      
      final response = await productProvider.getProductDetail(productId.value);
      if (response.isSuccess && response.data != null) {
        product.value = response.data;
      } else {
        Get.snackbar(
          '错误', 
          '获取产品详情失败: ${response.msg}',
          snackPosition: SnackPosition.BOTTOM,
        );
      }
    } catch (e) {
      debugPrint('加载产品详情失败: $e');
      Get.snackbar(
        '错误', 
        '获取产品详情失败: $e',
        snackPosition: SnackPosition.BOTTOM,
      );
    } finally {
      isLoading.value = false;
    }
  }
  
  // 获取节点类型名称
  String getNodeTypeName(int? nodeType) {
    switch (nodeType) {
      case 1:
        return '直连设备';
      case 2:
        return '网关设备';
      case 3:
        return '子设备';
      default:
        return '未知';
    }
  }
  
  // 获取数据格式名称
  String getDataFormatName(int? dataFormat) {
    switch (dataFormat) {
      case 1:
        return 'JSON';
      case 2:
        return '自定义';
      default:
        return '未知';
    }
  }
  
  // 获取认证类型名称
  String getAuthTypeName(int? authType) {
    switch (authType) {
      case 1:
        return '设备密钥';
      case 2:
        return '证书认证';
      default:
        return '未知';
    }
  }
  
  // 获取状态名称
  String getStatusName(int? status) {
    switch (status) {
      case 0:
        return '开发中';
      case 1:
        return '已发布';
      default:
        return '未知';
    }
  }
  
  // 刷新产品详情
  Future<void> refreshProductDetail() async {
    await loadProductDetail();
  }
} 